package de.tum.in.eist.poll.client.controllers;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.RootPanel;

import de.tum.in.eist.poll.client.views.EvaluationView;
import de.tum.in.eist.poll.client.views.LoginView;
import de.tum.in.eist.poll.client.views.PollView;
import de.tum.in.eist.poll.client.views.UserManagementView;
import de.tum.in.eist.poll.client.views.Views;
import de.tum.in.eist.poll.pub.service.PollService;
import de.tum.in.eist.poll.pub.service.PollServiceAsync;
import de.tum.in.eist.poll.pub.shared.User;
import de.tum.in.eist.poll.shared.beans.Question;
import de.tum.in.eist.poll.shared.beans.SingleChoiceQuestion;
import de.tum.in.eist.poll.shared.beans.TextQuestion;

/**
 * 
 * @author Sebastian Hofstetter
 *
 */
public class Controller {
	// TODO Factory implementieren
	public static Controller controller = GWT.create(Controller.class);
	public static LoginController loginController = GWT.create(LoginController.class);
	public static PollController pollController = GWT.create(PollController.class);
	public static UserManagementController userManagmentController = GWT.create(UserManagementController.class);
	public static EvaluationController evaluationController = GWT.create(EvaluationController.class);
	
	public User loggedInUser;
	
	public Views currentPage = Views.login;
	
	protected PollServiceAsync pollService = GWT.create(PollService.class);
	
	Question[] defaultQuestions = new Question[] {
			new SingleChoiceQuestion("How satisfied are you with today's lecture? (overall rating)", false, 
					new String[]{"Very unsatisfied", "Unsatisfied", "Neutral", "Satisfied", "Very Satisfied"}), 
			new TextQuestion("What did you like most?", false), 
			new TextQuestion("What did you dislike or did not understand at all?", false)};
	
	/**
	 * Factory
	 * @param controller
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 */
	
	/*
	static HashMap<Class<Controller>, Controller> controllers = new HashMap<Class<Controller>, Controller>();
	public static <T extends Controller> T create(Class<T> controller) {
		// TODO improve Code
		Class<Controller> k = (Class<Controller>) controller;
		Controller c = controllers.get(controller);
		if (c == null) {
			try {
				c = k.newInstance();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
			controllers.put(k, c);
		}
		
		return (T) c;
	}
	*/
	
	public void showCurrentPage() {
		if (loggedInUser == null) currentPage = Views.login;
		// Associate the Main panel with the HTML host page.
		RootPanel.get("poll").clear();
		switch(currentPage) {
		case login :
			RootPanel.get("poll").add(new LoginView());
			break;
		case evaluation:
			RootPanel.get("poll").add(new EvaluationView());
			break;
		case poll:
			RootPanel.get("poll").add(new PollView(defaultQuestions));
			break;
		case userManagement:
			RootPanel.get("poll").add(new UserManagementView());
			break;
		default:
			RootPanel.get("poll").add(new LoginView());
			break;
		}
	}
	
	public void showPage(Views page) {
		currentPage = page;
		showCurrentPage();
	}
}
